package DirectsDB;

import java.util.HashSet;

import org.hibernate.Session;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.List; 
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query; 

import DataBase.FileDB;
import DataBase.TagDB;


/**
* Description :the menu for the tables of Directs
* <p>
*
*@author Danielle
*/
public class MenuDirects {
	static  SessionFactory sessionFactory;
	/**
	* Description Ctor
	*/
	public MenuDirects()
	{
		sessionFactory = new Configuration().configure().buildSessionFactory();
	}
	
	/**
	* Description addDir
	* @param String dirName
	*/
	public void addDir(String dirName)
	{
		///open session
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		///if the session does not exist
		if(isDirExist(dirName))
		{
			session.close();
			return ;
		}
		///else:
		DirDB dir=new DirDB(dirName);// create new File object
		session.saveOrUpdate(dir); // save Tags
		session.getTransaction().commit();
		///close session
		session.close();
		
	}
	
	/**
	* Description isDirExist
	* @param String dirName
	* @return boolean
	*/
	public boolean isDirExist(String dirName)
	{
		///open session
		Session session = sessionFactory.openSession();
		///all the directs that their name: dirName
		Query query=session.createQuery("SELECT t from DirDB t WHERE t.dirName=:dirName");
		query.setParameter("dirName", dirName);
		List list=query.list();
		///if there is a object:
		if(!list.isEmpty())
		{
			///close session
			session.close();
			return true;
		}
		else
		{
			///close session
			session.close();
			return false;
		}
	}
	
	/**
	* Description delAll
	*/
	public void delAll()
	{
		///open session
		Session session = sessionFactory.openSession();
		///all the directs:
		Query query=session.createQuery("SELECT t from DirDB t");
		Iterator it;
		it=query.iterate();
		DirDB dir;
		///delete them
		while(it.hasNext())
		{
			dir=(DirDB)it.next();
			delDir(dir.getDirName());
		}		
		///close session
		session.close();
		
	}
//	public int retDirId()
//	{
//		return 0;
//	}
	/**
	* Description retAllDir
	* @return String[]
	*/
	public String[] retAllDir()
	{
		///open session
		Session session = sessionFactory.openSession();
		///all the directs:
		Query query=session.createQuery("SELECT t from DirDB t");
		Iterator it;
		it=query.iterate();
		DirDB dir;
		int size=query.list().size();
		String[] returnStr;
		///run over the list:
		returnStr=new String[size];
		int i=0;
		
		while(it.hasNext())
		{
			dir=(DirDB)it.next();
			returnStr[i]=dir.getDirName();
			i++;
		}
		session.close();
		return returnStr;
	}
	
	/**
	* Description delDir
	* @param String dirName
	* @return boolean
	*/
	public boolean delDir(String dirName)
	{
		///if the directs does not exist:
		if(!isDirExist(dirName))
			return false;
		///open session
		Session session = sessionFactory.openSession();
		///find the direct:
		Query query=session.createQuery("SELECT t from DirDB t WHERE t.dirName=:dirName");
		query.setParameter("dirName", dirName);
		
		Transaction transaction  = session.beginTransaction();
		 ///Delete the direct
		Iterator it=query.iterate();
		DirDB dir=(DirDB)it.next();
		session.delete(dir);
		transaction.commit();
		///close session
		session.close();
		return true;
		
	}

}
/*
 
 **/
 